﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI
{
	public partial class Board : UserControl
	{
		private int _gridCols;
		private int _gridRows;
		private float _boarder;
		private float _gridLineWeight;
		private int _cellSize;
		private int _zoom;
        private int _minBoardSize;

        private Pen blackPen;
        private Pen whitePen;

		#region Properties

		public int GridCols
		{
			get
			{
				return _gridCols;
			}
		}

		public int GridRows
		{
			get
			{
				return _gridRows;
			}
		}

		public float BoardBorder
		{
			get
			{
				return _boarder;
			}
			set
			{
				_boarder = value;
			}

		}

		public float GridLineWeight
		{
			get
			{
				return _gridLineWeight;
			}
			set
			{
				_gridLineWeight = value;
			}
		}

		public int CellSize
		{
			get
			{
				return _cellSize;
			}
		}

		public int Zoom
		{
			get
			{
				return _zoom;
			}
		}

		#endregion

		public Board() : this(20,20) { }

        public Board(int cols, int rows)
        {
            ChangeBoardSize(cols, rows);
			_cellSize = 25;
			_zoom = 50;
			_minBoardSize = 10;

            blackPen = new Pen(Color.Black, GridLineWeight);
            whitePen = new Pen(Color.White, GridLineWeight);

            this.BackColor = Color.Gray;

			init();
		}

		private void init()
		{
			InitializeComponent();

			_boarder = 3.0F;
			_gridLineWeight = 1.0F;
		}

		private void ZoomIn()
		{
		}

		private void ZoomOut()
		{
		}

        public void ChangeMinBoardSize(int square)
        {
            if (square >= 8)
            {
                _minBoardSize = square;
            }
        }

		public void ChangeBoardSize(int cols, int rows)
		{
            if (cols >= _minBoardSize)
			{
				_gridCols = cols;
			}
			else
			{
                _gridCols = _minBoardSize;
			}
            if (rows >= _minBoardSize)
			{
				_gridRows = rows;
			}
			else
			{
                _gridRows = _minBoardSize;
			}

            ///TODO: need to calculate cell size off of the board size
            ///board needs to be no less than 500 each direction
            ///board can be more than 500 and the Panel will stretch to accomidate

			this.CalculateBoardSize();

			this.Invalidate();
		}

		private void CalculateBoardSize()
		{
			Size tmpSize = new Size();
			
			tmpSize.Height= CellSize * GridRows;
			tmpSize.Width = CellSize * GridCols;

			this.Size = tmpSize;
		}

		private void GameBoard_Paint(object sender, PaintEventArgs e)
		{
			DrawBoarder(e.Graphics);

			DrawBoard(e.Graphics);
		}

		private void DrawBoarder(Graphics graphics)
		{
			using (Pen blackPen = new Pen(Brushes.Black, this.BoardBorder))
			{
				float centerLine = BoardBorder / 2;
				graphics.DrawRectangle(blackPen, centerLine, centerLine, this.Width - 2*centerLine, this.Height - 2*centerLine);
			}
		}

		private void PaintCell(Point origCorner, Color color)
		{

		}

		private void DrawBoard(Graphics graphics)
		{
			Point start = new Point(3, 0);
            Point end = new Point(this.Width - 3, 0);

			//Draw horizantal lines
			for (int i = 1; i < this.GridRows; i++)
			{
				start.Y = end.Y = CellSize * i;

				DrawLine(graphics, start, end, blackPen);

                start.Y += 1;
                end.Y += 1;

                DrawLine(graphics, start, end, whitePen);
			}

			start = new Point(0, 3);
            end = new Point(0, this.Height - 3);
			//Draw Vertical lines
			for (int i = 1; i < this.GridCols; i++)
			{
				start.X = end.X = CellSize * i;

				DrawLine(graphics, start, end, blackPen);

                start.X += 1;
                end.X += 1;

                DrawLine(graphics, start, end, whitePen);
			}
		}

		private void DrawLine(Graphics graphics, Point start, Point finish, Pen pen)
		{
			graphics.DrawLine(pen, start, finish);
		}

		private void Board_MouseClick(object sender, MouseEventArgs e)
		{
			///TODO: using the e.X, e.Y and FindCellLocation select the cell the mouse is over
		}

        private void Board_MouseMove(object sender, MouseEventArgs e)
        {
            ///TODO: using the e.X, e.Y, FindCellLocation and PaintCell add shadowing for the cell under the pointer
        }

	}
}
